//
//  MCLotteryRecordView.m
//  TLYL
//
//  Created by MC on 2017/6/21.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCLotteryRecordView.h"
#import "MCLotteryDrawDetailsViewController.h"
#import "UIView+MCParentController.h"
#import "MCMineCellModel.h"




#define WIDTH_LEFT  10
@interface MCLotteryRecordView ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray*marr_Section;
@property(nonatomic, strong)UIButton * btn_detail;
//@property(nonatomic, strong)MCLotteryRecordHeaderView * lotteryRecordHeaderView;
@property(nonatomic, strong)UIImageView * arrowImgV;

@end

@implementation MCLotteryRecordView
+(CGFloat)computeHeight:(id)info{
    return  350;
}
#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    
    self.backgroundColor=RGB(47, 127, 207);
    _marr_Section= [[NSMutableArray alloc]init];

//    [self addSubview:self.lotteryRecordHeaderView];
    
    [self addSubview:self.btn_detail];
    
    [self addSubview:self.tableView];
    
    [self layOutConstraints];
    
}


-(void)layOutConstraints{

//    [_lotteryRecordHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_top).offset(MC_REALVALUE(5));
//        make.left.and.right.equalTo(self).offset(0);
//        make.height.mas_equalTo([MCLotteryRecordHeaderView computeHeight:nil]);
//    }];
    
    
    [_btn_detail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.width.mas_equalTo(MC_REALVALUE(200));
        make.bottom.equalTo(self.mas_bottom).offset(-MC_REALVALUE(10));
        make.height.mas_equalTo(MC_REALVALUE(30));
    }];
    
    _arrowImgV=[[UIImageView alloc]init];
    [self addSubview:_arrowImgV];
    _arrowImgV.image=[UIImage imageNamed:@"ArrowMoreDown"];
    _arrowImgV.backgroundColor=[UIColor clearColor];
    [_arrowImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.width.mas_equalTo(16);
        make.bottom.equalTo(_btn_detail.mas_top).offset(0);
        make.height.mas_equalTo(9);
    }];

    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.and.right.equalTo(self).offset(0);
        make.bottom.equalTo(_btn_detail.mas_top).offset(-MC_REALVALUE(10));
    }];

    _arrowImgV.hidden=YES;
    _btn_detail.hidden=YES;
}

#pragma mark==============setDataSource=================
-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
    [_marr_Section removeAllObjects];
    
    NSArray * arr=dataSource;
    int  count = (int)arr.count;
    if (count>10) {
        count=10;
    }
    NSMutableArray * marr_Model=[[NSMutableArray alloc]init];
    for(int i=0 ;i<count; i++){
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = NSStringFromClass([MCLotteryRecordTableViewCell class]);
        model.className=NSStringFromClass([MCLotteryRecordTableViewCell class]);
        model.height = [MCLotteryRecordTableViewCell computeHeight:nil];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        /*
         * 传递参数
         */
        model.userInfo = arr[i];
        [marr_Model addObject:model];
    }
    SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:marr_Model];
    model0.headerhHeight=0.0001;
    model0.footerHeight=0.0001;
    [_marr_Section addObject:model0];
    [_tableView reloadData];
    _arrowImgV.hidden=NO;
    _btn_detail.hidden=NO;
    
}
#pragma mark-懒加载
-(UIButton *)btn_detail{
    if (!_btn_detail) {
        _btn_detail=[[UIButton alloc]init];
//        _btn_detail.backgroundColor=RGB(48, 127, 207);
        [_btn_detail setTitle:@"查看完整历史开奖" forState:UIControlStateNormal];
        [_btn_detail.titleLabel setFont:[UIFont systemFontOfSize:MC_REALVALUE(15)]];
        [_btn_detail addTarget:self action:@selector(action_detail) forControlEvents:UIControlEventTouchUpInside];
        [_btn_detail setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_detail.clipsToBounds=YES;
        _btn_detail.layer.cornerRadius=MC_REALVALUE(5);
    }
    return _btn_detail;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
//-(MCLotteryRecordHeaderView *)lotteryRecordHeaderView{
//    if (!_lotteryRecordHeaderView) {
//        _lotteryRecordHeaderView=[[MCLotteryRecordHeaderView alloc]init];
//    }
//    return _lotteryRecordHeaderView;
//}

#pragma mark-action_detail
-(void)action_detail{
    if ([self.delegate respondsToSelector:@selector(MCLookAllLotteryRecord)]) {
        [self.delegate MCLookAllLotteryRecord];
    }

}
#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _marr_Section.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.cells.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.headerhHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.footerHeight;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    return cm.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([MCLotteryRecordTableViewCell class])]) {
        
        MCLotteryRecordTableViewCell *ex_cell=(MCLotteryRecordTableViewCell *)cell;
        ex_cell.dataSource=cm.userInfo;
        
    }
    return cell;
}


@end
#pragma mark-Cell
@interface MCLotteryRecordTableViewCell ()
@property (nonatomic,strong)UILabel *lab_left;
@property (nonatomic,strong)UILabel *lab_right;
@property (nonatomic,strong)UIView *view_right;
@end

@implementation MCLotteryRecordTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    self.backgroundColor=[UIColor clearColor];
    UILabel * lab_left=[[UILabel alloc]init];
    [self addSubview:lab_left];
    _lab_left=lab_left;
    _lab_left.textAlignment=NSTextAlignmentLeft;
    _lab_left.text=@"加载中...";
    _lab_left.font=[UIFont systemFontOfSize:14];
    _lab_left.textColor=[UIColor whiteColor];
    [_lab_left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(WIDTH_LEFT);
        make.right.equalTo(self.mas_right).offset(0);
    }];
    
    _view_right=[[UIView alloc]init];
    [self addSubview:_view_right];
    [_view_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(G_SCREENWIDTH/2.0);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
//    UILabel * lab_right=[[UILabel alloc]init];
//    [self addSubview:lab_right];
//    _lab_right=lab_right;
//    _lab_right.text=@"加载中...";
//    _lab_right.textColor=[UIColor whiteColor];
//    _lab_right.font=[UIFont systemFontOfSize:18];
//    _lab_right.textAlignment=NSTextAlignmentCenter;
//    [_lab_right mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.mas_bottom).offset(0);
//        make.top.equalTo(self.mas_top).offset(0);
//        make.left.equalTo(self.mas_left).offset(G_SCREENWIDTH/2.0);
//        make.right.equalTo(self.mas_right).offset(-10);
//    }];
}
- (void)awakeFromNib {
    [super awakeFromNib];
}
    
-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
    NSDictionary * dic=dataSource;
    NSString * CzNum=dic[@"CzNum"];
    _lab_left.text=[NSString stringWithFormat:@"%@期",dic[@"CzPeriod"]];//@"0511023"
    [_view_right.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_view_right addSubview:[self GetViewWithNum:CzNum]];
//    _lab_right.text=[self GetCzNum:CzNum];//@"8，6，8，6，2"
}
//-(NSString *)GetCzNum:(NSString *)CzNum{
//    
//    CzNum=[CzNum stringByReplacingOccurrencesOfString:@"," withString:@" "];
//    
//    NSArray * realNumArr=@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
//    NSArray * showNumArr=@[@"⓪",@"①",@"②",@"③",@"④",@"⑤",@"⑥",@"⑦",@"⑧" ,@"⑨",@"⑩",@"⑪",@"⑫",@"⑬",@"⑭",@"⑮",@"⑯",@"⑰",@"⑱",@"⑲",@"⑳"];
//    for (int i=0; i<realNumArr.count; i++) {
//         CzNum=[CzNum stringByReplacingOccurrencesOfString:realNumArr[i] withString:showNumArr[i]];
//    }
//   
//
//   
//    
//    return CzNum;
//}

-(UIView*)GetViewWithNum:(NSString *)num{
    
    UIView * view=[[UIView alloc]init];
    CGFloat H = 30;
    view.frame=CGRectMake(0, 0, G_SCREENWIDTH/2.0-10, H);
    view.backgroundColor=[UIColor clearColor];
    NSArray * numArr=[num componentsSeparatedByString:@","];
    CGFloat W = 17;
    int count1 =(G_SCREENWIDTH/2.0 -10)/(W+10);
    int count2 =(int)numArr.count;
    count1 = floor(count1);
    int count=count2;
    if (count2>count1) {
        count=count1;
    }
    for (int i=0; i<count; i++) {
        UILabel * lab=[[UILabel alloc]init];
        lab.textColor=[UIColor whiteColor];
        lab.font=[UIFont systemFontOfSize:12];
        lab.clipsToBounds=YES;
        lab.textAlignment=NSTextAlignmentCenter;
        lab.layer.cornerRadius=W/2.0;
        lab.layer.borderColor = [UIColor whiteColor].CGColor;
        lab.layer.borderWidth = 1;
        lab.layer.masksToBounds = YES;
        lab.text=numArr[i];
        lab.frame=CGRectMake(i*(W+10), (H-W)/2.0, W, W);
        [view addSubview:lab];
    }
    if (count2>count1) {

        UILabel * labLast=[[UILabel alloc]init];
        labLast.textColor=[UIColor whiteColor];
        labLast.font=[UIFont systemFontOfSize:14];
        labLast.text=@"...";
        labLast.frame=CGRectMake(count*(W+10), (H-W)/2.0, W, W);
        [view addSubview:labLast];
    }
    return view;
    
}
+(CGFloat)computeHeight:(id)info{
    return 30;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}

@end
#pragma mark-Header
@interface MCLotteryRecordHeaderView ()
@end
@implementation MCLotteryRecordHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    UILabel * lab_left=[[UILabel alloc]init];
    [self addSubview:lab_left];
    lab_left.textAlignment=NSTextAlignmentLeft;
    lab_left.text=@"开奖期号";
    [lab_left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(WIDTH_LEFT);
        make.right.equalTo(self.mas_right).offset(0);
    }];
    
    UILabel * lab_right=[[UILabel alloc]init];
    [self addSubview:lab_right];
    lab_right.text=@"开奖号码";
    lab_right.textAlignment=NSTextAlignmentRight;
    [lab_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(-WIDTH_LEFT);
    }];

    
}
+(CGFloat)computeHeight:(id)info{
    return 30;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
